package com.taoyige.ui;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.taoyige.AppConfig;
import com.taoyige.AppContext;
import com.taoyige.AppException;
import com.taoyige.R;
import com.taoyige.bean.Order;
import com.taoyige.bean.User;
import com.taoyige.common.UIHelper;
import com.taoyige.widget.BadgeView;
import com.taoyige.widget.LoadingDialog;

/**
 * 用户个人中心
 * @author zhuxun
 * @version 1.0
 * @date 2014-11-25 上午9:56:40
 */
public class UserActivity extends BaseActivity {
	
	private AppContext appContext;
	private Handler handler;
	private LoadingDialog loading;
	
	private ImageButton returnBack;
	private ImageButton homePage;
	private TextView userHeadText;
	private RelativeLayout relativeLayout;
	private RelativeLayout consulationlayout,aboutLayout;
	private LinearLayout unpaidOrder;
	private LinearLayout notReceiptOrder;
	private LinearLayout allOrder;
	private Button signout;
	private RelativeLayout callnumber;
	private Integer badgeFlag;
	private RelativeLayout signoutRe;
	
	private LinearLayout unPaidIV;
	private LinearLayout notReceiptIV;
	private LinearLayout allOrderIV;
	private LinearLayout userbody;
	private ImageView user_name_avatar;
	private TextView user_name_text2;
	
	private User user;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_main);
		appContext=(AppContext) getApplication();
		badgeFlag=getIntent().getIntExtra("badgeFlag", 0);
		this.initView();
		// 网络连接判断
		if (!appContext.isNetworkConnected()){
			signoutRe.setVisibility(View.INVISIBLE);
			userbody.setVisibility(View.INVISIBLE);
			return;
		}		
		this.initData();
	}
	
	private void initView(){
		returnBack=(ImageButton)findViewById(R.id.user_hear_return_back);
		homePage=(ImageButton)findViewById(R.id.user_hear_homePage);
		userHeadText=(TextView)findViewById(R.id.user_hear_text);
		userHeadText.setText("个人中心");
		signoutRe=(RelativeLayout)findViewById(R.id.signoutRe);
		
		relativeLayout=(RelativeLayout)findViewById(R.id.user_main_address_main);
		consulationlayout = (RelativeLayout)findViewById(R.id.user_main_consulation);
		aboutLayout = (RelativeLayout)findViewById(R.id.user_main_about);
		userbody=(LinearLayout)findViewById(R.id.user_main_body);
		
		user_name_avatar=(ImageView) findViewById(R.id.user_name_avatar);
		user_name_text2=(TextView) findViewById(R.id.user_name_text2);
		
		unpaidOrder=(LinearLayout)findViewById(R.id.user_status1);
		notReceiptOrder=(LinearLayout)findViewById(R.id.user_status2);
		allOrder=(LinearLayout)findViewById(R.id.user_status3);
		signout=(Button)findViewById(R.id.signout);
		
		unPaidIV=(LinearLayout) findViewById(R.id.user_status11);
		notReceiptIV=(LinearLayout) findViewById(R.id.user_status22);
		allOrderIV=(LinearLayout) findViewById(R.id.user_status33);
		
		returnBack.setOnClickListener(homeListener);
		homePage.setOnClickListener(homeListener);
		
		relativeLayout.setOnClickListener(addressListener);
		consulationlayout.setOnClickListener(consulationListener);
		aboutLayout.setOnClickListener(aboutListener);
		
		unpaidOrder.setOnClickListener(unpaidOrderListener);
		notReceiptOrder.setOnClickListener(notReceiptOrderListener);
		allOrder.setOnClickListener(allOrderListener);
		
		signout.setOnClickListener(logoutListener);
		
		callnumber=(RelativeLayout)findViewById(R.id.user_main_call_number);
		callnumber.setOnClickListener(callNumberClickListener);
		switch (badgeFlag) {
		case AppConfig.BADGE_NO:
			break;
		case AppConfig.BADGE_ORDER_NOPAID:
			BadgeView.showBadge(UserActivity.this,unPaidIV,"1");
			break;
		case AppConfig.BADGE_ORDER_PAID:
			BadgeView.showBadge(UserActivity.this,notReceiptIV,"1");
			break;
		}
	}
	

	private void initData() {
		handler = new Handler() {
			public void handleMessage(Message msg) {
				if (loading != null)
					loading.dismiss();
				if (msg.what == 1 && msg.obj != null) {
					user=(User)msg.obj;
					user_name_text2.setText(user.getpUserName());
					switch (user.getPid()) {
					case AppConfig.LOGIN_OAUTH_WECHAT:
						user_name_avatar.setImageResource(R.drawable.weix);
						break;
					case AppConfig.LOGIN_OAUTH_QQ:
						user_name_avatar.setImageResource(R.drawable.qq);
						break;
					case AppConfig.LOGIN_OAUTH_WEIBO:
						user_name_avatar.setImageResource(R.drawable.weibo);
						break;
					case AppConfig.LOGIN_OAUTH_ALIPAY:
						user_name_avatar.setImageResource(R.drawable.zhifubao);
						break;
					}
				} else if (msg.obj != null) {
					((AppException) msg.obj).makeToast(UserActivity.this);
				}
				
			}
		};

		loading = new LoadingDialog(this);
		loading.show();
		
		new Thread() {
			public void run() {
				Message msg = new Message();
				try {
					User user=appContext.getLoginInfo();
					msg.what = 1;
					msg.obj = user;
				} catch (AppException e) {
					e.printStackTrace();
					msg.what = -1;
					msg.obj = e;
				}
				handler.sendMessage(msg);
			}
		}.start();
	}
	private View.OnClickListener addressListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			Order order = new Order(); 
			order.setTotalPrice(0.0001);
			Intent intent = new Intent(UserActivity.this, AddressDetailActivity.class);
			intent.putExtra("order", order);
			UserActivity.this.startActivity(intent);
		}
	};
	/**
	 * 在线咨询
	 */
	private View.OnClickListener consulationListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			Intent intent = new Intent(UserActivity.this,ConsulationActivity.class);
			startActivity(intent);
		}
	};	
	/**
	 * 关于淘一个 onClick
	 */
	private View.OnClickListener aboutListener = new View.OnClickListener() {		
		@Override
		public void onClick(View v) {
			Intent itent = new Intent(UserActivity.this,AboutTaoyigeActivity.class);			
			startActivity(itent);
		}
	};
	private View.OnClickListener logoutListener = new View.OnClickListener() {
		@Override
		public void onClick(final View v) {
			
			AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
			builder.setIcon(android.R.drawable.ic_dialog_info);
			//builder.setTitle(R.string.msg_order_cancel_title);
			builder.setMessage(R.string.app_menu_surelogout);
			builder.setPositiveButton(R.string.sure,
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
							appContext.logout();
							UIHelper.ToastMessage(UserActivity.this, getString(R.string.msg_logout));
							UIHelper.showHomePage(v.getContext());
							finish();
						}
					});
			builder.setNegativeButton(R.string.cancle,
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					});
			builder.show();
			
			
		}
	};
	
	private View.OnClickListener homeListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			UIHelper.showHomePage(v.getContext());
			finish();
		}
	};
	
	private View.OnClickListener callNumberClickListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			Intent intent=new Intent();
			intent.setAction(Intent.ACTION_DIAL);
			intent.setData(Uri.parse("tel:4000024578"));
			startActivity(intent);
		}
	};
	
	private View.OnClickListener notReceiptOrderListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			UIHelper.showNotReceiptOrder(v.getContext());
			finish();
		}
	};
	
	private View.OnClickListener unpaidOrderListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			UIHelper.showUnpaidOrder(v.getContext());
			finish();
		}
	};
	
	private View.OnClickListener allOrderListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			UIHelper.showAllOrder(v.getContext());
		}
	};
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		boolean flag = true;
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			UIHelper.showIndex(UserActivity.this);
			finish();
		} 
		return flag;
	}
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return false;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
